/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.JmsSupport;
import han.bifang.gwt.client.common.entity.JmsBinding;
import han.bifang.gwt.client.common.entity.JmsBindingWrapper;
import han.bifang.gwt.client.common.entity.MqJms;
import han.bifang.gwt.client.common.entity.NormalJms;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class JmsPanel extends Composite {

	private ServiceMessages cons;

	private JmsBindingWrapper w;

	private SimplePanel infoPanel;

	private TextBox user;

	private PasswordTextBox passwd;

	private ListBox jmsType, jmsServer, jmsVersion;

	private NormalJmsPanel normalJmsPanel;

	private MqJmsPanel mqJmsPanel;

	public JmsPanel(ServiceMessages scons, JmsBindingWrapper b) {
		this.cons = scons;
		this.w = b;
		VerticalPanel panel = new VerticalPanel();
		panel.setWidth("100%");
		infoPanel = new SimplePanel();
		infoPanel.setWidth("100%");

		Grid jms = new Grid(5, 2);
		jms.setCellPadding(1);
		jms.setCellSpacing(1);
		jms.setBorderWidth(1);

		ColumnFormatter cf = jms.getColumnFormatter();
		cf.setWidth(0, "15%");
		cf.setWidth(1, "55%");

		CellFormatter ff = jms.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		ff.setStyleName(2, 0, "tablecol");
		ff.setStyleName(3, 0, "tablecol");
		ff.setStyleName(4, 0, "tablecol");

		jms.setHTML(0, 0, cons.jmsProvider());
		jms.setHTML(1, 0, cons.version());
		jms.setHTML(2, 0, cons.jmsType());
		jms.setHTML(3, 0, cons.user());
		jms.setHTML(4, 0, cons.password());

		jmsType = new ListBox();
		jmsType.addItem(cons.queue(), EntityConstants.JMS_TYPE_QUEUE);
		jmsType.addItem(cons.topic(), EntityConstants.JMS_TYPE_TOPIC);

		jmsServer = new ListBox();

		jmsVersion = new ListBox();

		

		user = new TextBox();
		user.setVisibleLength(32);

		passwd = new PasswordTextBox();
		passwd.setVisibleLength(32);

		if (w != null) {
			JmsBinding jb = w.getJmsBinding();
			String[] names = JmsSupport.getSupportProviderNameList();

			for (int i = 0, j = names.length; i < j; i++) {
				jmsServer.addItem(names[i], JmsSupport.getProvidersValue(names[i]));
				if (jb.getServer().equals(names[i])) {
					jmsServer.setSelectedIndex(i);
				}
			}
			ArrayList<String> v = JmsSupport.getSupportVersion(jb.getServer());
			for (int i = 0, j = v.size(); i < j; i++) {
				jmsVersion.addItem(v.get(i), v.get(i));
				if (jb.getServerVersion().equals(v.get(i))) {
					jmsVersion.setSelectedIndex(i);
				}
			}
			if (jb.getJmsType().equals(EntityConstants.JMS_TYPE_QUEUE)) {
				jmsType.setSelectedIndex(0);
			} else {
				jmsType.setSelectedIndex(1);
			}

			if (!StringUtils.isEmpty(jb.getJmsUser())) {
				user.setText(jb.getJmsUser());
				passwd.setText(jb.getJmsPassword());
			}

			if (!jb.getServer().equals("WebSphere MQ")) {
				normalJmsPanel = new NormalJmsPanel(cons, (NormalJms) w
						.getInfo());
				infoPanel.setWidget(normalJmsPanel);
			} else {
				mqJmsPanel = new MqJmsPanel(cons, (MqJms) w.getInfo());
				infoPanel.setWidget(mqJmsPanel);
			}
		} else {
			String[] names = JmsSupport.getSupportProviderNameList();

			for (int i = 0, j = names.length; i < j; i++) {
				jmsServer.addItem(names[i], JmsSupport.getProvidersValue(names[i]));
			}
			jmsServer.setItemSelected(0, true);
			
			//Window.alert(jmsServer.getValue(0));
			ArrayList<String> v = JmsSupport.getSupportVersion(jmsServer
					.getValue(0));
			for (int i = 0, j = v.size(); i < j; i++) {
				jmsVersion.addItem(v.get(i), v.get(i));

			}
			jmsType.setSelectedIndex(0);
			
			jmsVersion.setItemSelected(0, true);
			
			normalJmsPanel = new NormalJmsPanel(cons, null);
			infoPanel.setWidget(normalJmsPanel);
		}
		jmsServer.addChangeListener(new ChangeListener() {

			public void onChange(Widget sender) {
				ListBox t = (ListBox) sender;
				jmsVersion.clear();
				ArrayList<String> v = JmsSupport.getSupportVersion(t
						.getValue(t.getSelectedIndex()));
				for (String ver : v) {
					jmsVersion.addItem(ver, ver);
				}
				jmsVersion.setItemSelected(0, true);
				//Window.alert(t.getValue(t.getSelectedIndex()));
				if(t.getValue(t.getSelectedIndex()).equals("WebSphere MQ")){
					normalJmsPanel = null;
					if(w != null && w.getInfo() instanceof MqJms){
						mqJmsPanel = new MqJmsPanel(cons, (MqJms) w.getInfo());
					}else {
						mqJmsPanel = new MqJmsPanel(cons, null);
					}
					infoPanel.setWidget(mqJmsPanel);
				}else {
					mqJmsPanel = null;
					if(w != null && w.getInfo() instanceof NormalJms){
						normalJmsPanel = new NormalJmsPanel(cons,(NormalJms)w.getInfo());
					}else{
						normalJmsPanel = new NormalJmsPanel(cons,null);
					}
					infoPanel.setWidget(normalJmsPanel);
				}

			}

		});
		jms.setWidget(0, 1, jmsServer);
		jms.setWidget(1, 1, jmsVersion);
		jms.setWidget(2, 1, jmsType);

		jms.setWidget(3, 1, user);
		jms.setWidget(4, 1, passwd);

		panel.add(jms);

		panel.add(new HTML("&nbsp;"));

		panel.add(new HTML(cons.communicateInfo()));

		panel.add(infoPanel);

		initWidget(panel);

	}
	
	public JmsBindingWrapper getJmsBindingWrapper(){
		if (!StringUtils.lengthLess(user.getText(), 32)) {
			Window.alert(cons.lengthError(cons.jmsServerUrl(), "0", "32"));
			return null;
		}
		if (!StringUtils.lengthLess(passwd.getText(), 20)) {
			Window.alert(cons.lengthError(cons.jmsServerUrl(), "0", "20"));
			return null;
		}
		if(w == null){
			w = new JmsBindingWrapper();
		}
		JmsBinding jb = new JmsBinding();
		jb.setServer(jmsServer.getValue(jmsServer.getSelectedIndex()));
		jb.setServerVersion(jmsVersion.getValue(jmsVersion.getSelectedIndex()));
		jb.setJmsPassword(passwd.getText());
		jb.setJmsUser(user.getText());
		jb.setJmsType(jmsType.getValue(jmsType.getSelectedIndex()));
		
		w.setJmsBinding(jb);
		
		if(jb.getServer().equals("WebSphere MQ")){
			MqJms t = mqJmsPanel.getMqJms();
			if(t == null)
				return null;
			w.setInfo(t);
		}else{
			NormalJms t =  normalJmsPanel.getNormalJms();
			if(t == null)
				return null;
			w.setInfo(t);
		}
		return w;
	}

}
